import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class GreaterIntComp implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o1 -o2 ;
    }
}
public class hw {
    public int[] smallestK(int[] arr, int k) {
        if(arr== null||k<0){
            return new int[k];
        }
        GreaterIntComp greaterIntComp = new GreaterIntComp();
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(greaterIntComp);
        for(int i = 0; i < k; i++){
            maxHeap.offer(arr[i]);
        }
        for(int i = k; i < arr.length; i++){
            int top = maxHeap.peek();
            if(arr[i]<top){
                maxHeap.poll();
                maxHeap.offer(arr[i]);
            }
        }
        int[] ret = new int[k];
        for(int i =0 ;i < k; i++){
            int val = maxHeap.poll();
            ret[i] = val;
        }
        return ret;
    }
}
